package cn.harry.wx.service.impl;

import cn.harry.wx.dao.WxTemplateMsgLogDao;
import cn.harry.wx.entity.WxTemplateMsgLog;
import cn.harry.wx.service.WxTemplateMsgLogService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * 微信模版消息发送记录
 *
 * @author honghh
 * Date 2021-04-08 16:19:27
 * Copyright (C) www.tech-harry.cn
 */
@Service("wxTemplateMsgLogService")
public class WxTemplateMsgLogServiceImpl extends ServiceImpl<WxTemplateMsgLogDao, WxTemplateMsgLog> implements WxTemplateMsgLogService {


    @Override
    public IPage<WxTemplateMsgLog> getPage(WxTemplateMsgLog wxTemplateMsgLog, Integer pageSize, Integer pageNum) {
        LambdaQueryWrapper<WxTemplateMsgLog> wrapper = new LambdaQueryWrapper<>(wxTemplateMsgLog);
        if (StrUtil.isNotEmpty(wxTemplateMsgLog.getBeginTime())) {
            wrapper.gt(WxTemplateMsgLog::getCreateTime, wxTemplateMsgLog.getBeginTime());
        }
        if (StrUtil.isNotEmpty(wxTemplateMsgLog.getEndTime())) {
            wrapper.lt(WxTemplateMsgLog::getCreateTime, wxTemplateMsgLog.getEndTime());
        }
        return page(new Page<>(pageNum, pageSize), wrapper);
    }


    @Override
    public WxTemplateMsgLog selectById(Long id) {
        return this.baseMapper.selectById(id);
    }
    
    @Override
    public int create(WxTemplateMsgLog wxTemplateMsgLog) {
        return this.baseMapper.insert(wxTemplateMsgLog);
    }

 
    @Override
    public int update(Long id, WxTemplateMsgLog wxTemplateMsgLog) {
        wxTemplateMsgLog.setId(id);
        return this.baseMapper.updateById(wxTemplateMsgLog);
    }


    @Override
    public int deleteByIds(Long[] ids) {
        return this.baseMapper.deleteBatchIds(Arrays.asList(ids));
    }


    @Override
    @Async
    public void addLog(WxTemplateMsgLog log) {
        this.baseMapper.insert(log);
    }
}